package org.lanqiao.api.product;

import java.io.IOException;
import org.lanqiao.common.Constants;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.pojo.ProductPOJO;
import org.lanqiao.service.ProductServiceImpl;

/**
 * Servlet implementation class AddProductServlet
 */
@WebServlet("/api/product/addproduct")
public class AddProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;  
    /**
     * @see 上新商品
     */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductPOJO product=new ProductPOJO();
		//前台传入的数据名product api  /api/product/addproduct 成功返回整数
		product.setProductCode(request.getParameter("productCode"));//商品编号
		product.setProductName(request.getParameter("productName"));//商品名称
		product.setProductColor(request.getParameter("productColor"));//商品颜色
		product.setStandard(request.getParameter("standard"));//商品规格
		product.setUnit(request.getParameter("unit"));//商品单位
		product.setPrice(request.getParameter("price"));//商品价格
		product.setCategoryId(request.getParameter("categoryId"));//商品类别ID
		product.setBrandId(request.getParameter("brandId"));//商品品牌ID
		product.setPhoto(request.getParameter("photo"));//商品照片
		product.setOrder(request.getParameter("order"));//货物量
		product.setSuggestPrice(request.getParameter("suggestPrice"));//建议价格
		product.setProductComment(request.getParameter("comment"));//商品描述
		JsonResult result=null;
		try {
			ProductServiceImpl productService=new ProductServiceImpl();
			int list=productService.addProductService(product);
			if(list>0) {
				result=new JsonResult("上架成功",Constants.STATUS_SUCCESS,list);
			}else {
				result=new JsonResult("上架失败",Constants.STATUS_UNFOUND);
			}
		}catch(Exception e){
			result=new JsonResult("上架异常",Constants.STATUS_FAILURE);
		}
		
		JsonResultWriter.writer(response,result);
	}
	
}
